{ "cells": [ { "cell_type": "markdown", "id": "6b0ec9e1", "metadata": {}, "source": [ "# Ger radar rainfall along CML paths" ] }, { "cell_type": "code", "execution_count": 1, "id": "49388feb", "metadata": {}, "outputs": [], "source": [ "import pycomlink as pycml\n", "import xarray as xr\n", "import matplotlib.pyplot as plt\n", "import poligrain as plg" ] }, { "cell_type": "markdown", "id": "415dca87", "metadata": {}, "source": [ "## Load example data" ] }, { "cell_type": "code", "execution_count": 2, "id": "9eaab2d5", "metadata": {}, "outputs": [], "source": [ "data_path = pycml.io.examples.get_example_data_path()\n", "cmls = xr.open_dataset(data_path + '/example_cml_data.nc')" ] }, { "cell_type": "code", "execution_count": 3, "id": "08efe99f", "metadata": {}, "outputs": [], "source": [ "ds_radar = xr.open_dataset(data_path + '/example_areal_reference_data.nc')" ] }, { "cell_type": "markdown", "id": "3ddb93dd", "metadata": {}, "source": [ "## Calculate intersection weights\n", "\n", "Note that the intersection weights are stored as `sparse.arrays` in a `xarray.DataArray` because the matrix of intersection weights for each CMLs, based on the radar grid, contains mostly zeros. Hence, we can save a lot of space. For large CML networks this is crucial because storing thousands of intersection weight matrices, one for each CML, easily eats up 10s of GBs of memory.\n", "\n", "Note that this calculation is fairly fast, i.e. approx. 2 seconds for 500 CMLs." ] }, { "cell_type": "code", "execution_count": 4, "id": "d89aefb7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 2.39 s, sys: 29.4 ms, total: 2.42 s\n", "Wall time: 2.44 s\n" ] } ], "source": [ "%%time \n", "\n", "da_intersect_weights = plg.spatial.calc_sparse_intersect_weights_for_several_cmls(\n", " x1_line=cmls.site_a_longitude.values,\n", " y1_line=cmls.site_a_latitude.values,\n", " x2_line=cmls.site_b_longitude.values,\n", " y2_line=cmls.site_b_latitude.values,\n", " cml_id=cmls.cml_id.values,\n", " x_grid=ds_radar.longitudes.values,\n", " y_grid=ds_radar.latitudes.values,\n", " grid_point_location='lower_left',\n", ")" ] }, { "cell_type": "code", "execution_count": 5, "id": "ac984c71", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray (cml_id: 500, y: 190, x: 228)> Size: 155kB\n",
"<COO: shape=(500, 190, 228), dtype=float64, nnz=4842, fill_value=0.0>\n",
"Coordinates:\n",
" x_grid (y, x) float64 347kB 1.23 1.24 1.25 1.27 ... 4.14 4.15 4.17 4.18\n",
" y_grid (y, x) float64 347kB 56.88 56.88 56.88 56.88 ... 58.48 58.48 58.47\n",
" * cml_id (cml_id) <U3 6kB '0' '1' '2' '3' '4' ... '496' '497' '498' '499'\n",
"Dimensions without coordinates: y, x<xarray.DataArray (time: 3168, cml_id: 500)> Size: 13MB\n",
"array([[0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" ...,\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.]])\n",
"Coordinates:\n",
" * time (time) datetime64[ns] 25kB 2018-05-10 ... 2018-05-20T23:55:00\n",
" * cml_id (cml_id) <U3 6kB '0' '1' '2' '3' '4' ... '496' '497' '498' '499'